package cn.lixufeng.system.controller;

import cn.lixufeng.basic.utils.AjaxResult;
import cn.lixufeng.basic.utils.PageList;
import cn.lixufeng.system.domain.DictionaryDetail;
import cn.lixufeng.system.query.DictionaryDetailQuery;
import cn.lixufeng.system.service.IDictionaryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionaryDetail")
public class DictionaryDetailController {

    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    /**
     * 增加或修改
     * @param dictionaryDetail
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody DictionaryDetail dictionaryDetail){
        try {
            if (dictionaryDetail.getId() == null) {
                dictionaryDetailService.add(dictionaryDetail);
            } else {
                dictionaryDetailService.update(dictionaryDetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    /**
     * 按id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            dictionaryDetailService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    /**
     * 按id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public DictionaryDetail findById(@PathVariable("id") Long id){
        return dictionaryDetailService.findById(id);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/listAll")
    public List<DictionaryDetail> findAll(){
        return dictionaryDetailService.findAll();
    }

    /**
     * 分页和高级查询
     * @param dictionaryDetailQuery
     * @return
     */
    @PostMapping("/listByPage")
    public PageList<DictionaryDetail> findPage(@RequestBody DictionaryDetailQuery dictionaryDetailQuery){
        return dictionaryDetailService.findPage(dictionaryDetailQuery);
    }

    /**
     * 批量删除
     * @param ids 是需要删除的id的集合，前台传的是数组，后台可以用数组或者集合来接收
     * @return
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            dictionaryDetailService.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

}
